import axios from 'axios'
import { getToken } from '@/utils/auth'
import router from '@/router'
// 创建axios实例
const service = axios.create({
  timeout: 10000
})

// request拦截器
service.interceptors.request.use(config => {
  config.headers['Authorization'] = getToken()
  return config
}, error => {
  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    /**
    * code为非200是抛错 可结合自己业务进行修改
    */
    const res = response.data
    // 401:未登录;
    if (res.code === 401) {
      router.push('/login')
    }
    return response.data
  },
  error => {
    console.log('err' + error)// for debug
  }
)

export default service
